#include "stdafx.h"
#include <QtCore/QCoreApplication>
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <iomanip>

class Student {
private:
    std::string name;
    int id;
    double score;

public:
    Student(std::string n, int i, double s) : name(n), id(i), score(s) {}

    std::string getName() const { return name; }
    int getId() const { return id; }
    double getScore() const { return score; }

    void display() const {
        std::cout << std::setw(10) << id << std::setw(15) << name
            << std::setw(10) << std::fixed << std::setprecision(2) << score << std::endl;
    }
};

class StudentManager {
private:
    std::vector<Student> students;

public:
    void addStudent() {
        std::string name;
        int id;
        double score;

        std::cout << "输入学生信息:" << std::endl;
        std::cout << "学号: "; std::cin >> id;
        std::cout << "姓名: "; std::cin >> name;
        std::cout << "成绩: "; std::cin >> score;

        students.emplace_back(name, id, score);
        std::cout << "添加成功!" << std::endl;
    }

    void displayAll() const {
        if (students.empty()) {
            std::cout << "没有学生记录!" << std::endl;
            return;
        }

        std::cout << std::setw(10) << "学号" << std::setw(15) << "姓名"
            << std::setw(10) << "成绩" << std::endl;
        std::cout << std::string(35, '-') << std::endl;

        for (const auto& student : students) {
            student.display();
        }
    }

    void searchById() const {
        int id;
        std::cout << "输入要查找的学号: "; std::cin >> id;

        for (const auto& student : students) {
            if (student.getId() == id) {
                student.display();
                return;
            }
        }
        std::cout << "未找到该学生!" << std::endl;
    }

    void sortByScore() {
        std::sort(students.begin(), students.end(),
            [](const Student& a, const Student& b) {
                return a.getScore() > b.getScore();
            });
        std::cout << "按成绩排序完成!" << std::endl;
    }

    void showMenu() {
        std::cout << "\n=== 学生成绩管理系统 ===" << std::endl;
        std::cout << "1. 添加学生" << std::endl;
        std::cout << "2. 显示所有学生" << std::endl;
        std::cout << "3. 按学号查找" << std::endl;
        std::cout << "4. 按成绩排序" << std::endl;
        std::cout << "5. 退出系统" << std::endl;
        std::cout << "请选择操作: ";
    }

    void run() {
        int choice;
        while (true) {
            showMenu();
            std::cin >> choice;

            switch (choice) {
            case 1: addStudent(); break;
            case 2: displayAll(); break;
            case 3: searchById(); break;
            case 4: sortByScore(); displayAll(); break;
            case 5: std::cout << "谢谢使用!" << std::endl; return;
            default: std::cout << "无效选择!" << std::endl;
            }
        }
    }
};


int main(int argc, char *argv[])
{
    QCoreApplication app(argc, argv);
    StudentManager manager;
    manager.run();
    return app.exec();
}
